package com.qf;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MyFactory {
    private Properties properties;
    public MyFactory() {
        InputStream inputStream = MyFactory.class.getResourceAsStream("/bean.properties");
        properties=new Properties();
        try {
            properties.load(inputStream);
            System.out.println("properties = " + properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //提供一个根据传入的名字获取对象实例的方法
    public Object getBean(String name) throws Exception {
        String classPath = (String) properties.get(name);
        System.out.println("classPath = " + classPath);
        //使用反射构造一个类的实例
        Class cls = Class.forName(classPath);
        //newInstance 获取类的实例
        return cls.newInstance();
    }
}
